import AntModal from '@/components/pt/dialog/AntModal'
import { addAccessory,listBizOrder } from '@/api/biz/bizOrder'
import { upload } from '@/api/system/upload'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import BizMeasureListForm from '@/views/biz/bizmeasure/modules/BizMeasureListForm';
import { getBizMeasure, updateBizMeasure } from '@/api/biz/bizMeasure'
export default {
  name: 'BizMeasureForm',

  components: {
    AntModal,
    BizMeasureListForm
  },
  data() {
    return {
      showProductModal: false,
      showPlanModal: false,
      activeKey: "1",
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      // 设置上传的请求头部
      headers: { Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
      multiple: false,
      multipleDetail:false,
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      }, 
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      loading: false,
      disabled: true,
      total: 0,
      id: undefined,
      formTitle: '测量信息',
      // 表单参数
      form: {orderType:"1"},
      attachmentRefName: 'addUploaderFile', // 标志表单是否含有附件
      formId: '',
      currentType:'',//当前新增明细类型
      list: [],
      selectedRowKeys: [],
      selectedDetailRowKeys: [],
      selectedDetailRows:[],
      ids: [],
      fileList: [],
      orderId:'',
      bizMeasureId:'',
      bizMeasureInfo:{},
    }
  },
  filters: {},
  created() { 
  },
  computed: {},
  watch: {},
  mounted() { },
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      console.log('cancel')
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        remark: undefined,
        orderId:undefined
      }
      this.fileList = []
      this.selectedRowKeys = []
    },

    /** 新增按钮操作 */
    handleSetMeasure(row) {
      this.open = true
      this.orderId=row.id;
      this.fileList=[];
      this.spinning = !this.spinning
      this.bizMeasureId = row.measureId
      this.bizMeasureInfo = row
      if (this.bizMeasureId) {
        getBizMeasure(this.bizMeasureId).then(res => {    
          if(res.code == 200) {
            this.form = res.data
            let fileList = (res.data && res.data.fileList || '').split(',')
            fileList.forEach((item,index)=>{
              const d = {
                uid: index,
                name: item,
                url: item,
                status: 'done'
              }
              // 文件列表
              this.fileList.push(d)
              console.log('this.fileList :>> ', this.fileList);
              this.spinning = !this.spinning
            })
            this.$forceUpdate()
          }else{
            this.message.error(res.msg)
          }
        })
      }
      

    },
    getDetail(row){
      
      
    },
    modify(){
      this.disabled = false
      
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      // this.disabled = true
      let filePaths="";
      this.fileList.forEach(file=>{
        if(filePaths.length>0) {
          filePaths+=",";
        }
        filePaths+=file.url;
      })
      let saveForm={
        fileList:filePaths,
        status:1,
        orderId:this.orderId,
        measureStatus: +!!this.fileList.length
      }
      saveForm = Object.assign(this.form,saveForm)
      updateBizMeasure(saveForm).then(response=>{
        this.$message.success('保存成功', 3)
        this.open = false
        this.$emit('ok')
        this.$emit('close')
      })
    },
    handleChange(info,e) {
      const formData = new FormData();
      formData.append('file', info.file);
      upload(formData).then(response => {
        const d = {
          uid: info.file.uid,
          name: response.fileName,
          url: response.url,
          status: 'done'
        }
        this.fileList.push(d);
    })
    },
    handleRemove(info) {
      this.fileList = this.fileList.filter(function (item) {
        return item.uid != info.uid
      })
    },
    /**
     * 关联测量单据
     */
    handleChooseMeasure(){
      this.$nextTick(()=>{
        this.$refs.BizMeasureListForm.handleChooseMeasure(this.bizMeasureInfo)
      })
    },
    selectOk(rows){
      this.form = rows[0]
      let fileList = (this.form && this.form.fileList || '').split(',')
      fileList.forEach((item,index)=>{
        const d = {
          uid: index,
          name: item,
          url: item,
          status: 'done'
        }
        // 文件列表
        this.fileList.push(d)
        console.log('this.fileList :>> ', this.fileList);
        this.spinning = !this.spinning
      })
      this.$forceUpdate()
    },
  }
}
